//定制请求的实例

//导入axios  npm install axios
import axios from 'axios';
import { ElMessage } from 'element-plus'
//定义一个变量,记录公共的前缀  ,  baseURL
const baseURL = '/api';
const instance = axios.create({baseURL})

import {useTokenStore} from "@/stores/token.js";
// import router from "@/router";
import {useRouter} from "vue-router";
instance.interceptors.request.use(
    (config)=>{
        const tokenStore = useTokenStore();
        if (tokenStore.token){
            config.headers['token'] = `${tokenStore.token}`;
        }
        return config;
    },
    (err) =>{
        return Promise.reject(err);
    }
)

//添加响应拦截器
instance.interceptors.response.use(
    result=>{
        //判断业务状态吗
        if(result.data.code === 200){
            return result.data;
        }
        ElMessage.error(result.data.message?result.data.message:'服务异常');
        return Promise.reject(result.data);//异步的状态转化成失败的状态
    },
    err=>{
        if (err.response.status===401){
            ElMessage.error("请先登录")
            const router=useRouter()
            router.push('/login')
        }
        ElMessage.error('服务异常');
        return Promise.reject(err);//异步的状态转化成失败的状态
    }
)

export default instance;
